package com.asset.modules.common.servcie;

import com.alibaba.excel.event.AnalysisEventListener;
import com.asset.modules.assets.model.Asset;
import com.asset.modules.assets.service.AssetService;
import com.asset.modules.budget.model.Budget;
import com.asset.modules.budget.service.BudgetService;
import com.asset.modules.common.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenchen
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ExcelService {

    @Autowired
    AssetService as;
    @Autowired
    BudgetService bs;



    /**
     * Excel文件读取
     * @param filePath
     * @param c
     * @param listener
     * @param sheetNo
     * @param sheetName
     * @param headRowNum
     */
    public String  readExcel(String filePath, Class c, AnalysisEventListener listener, int sheetNo, String sheetName, int headRowNum){
       return OperateExcelService.ParseExcel(filePath, c, listener, sheetNo,sheetName, headRowNum);
    }


    /**
     * Excel文件读取
     * @param file
     * @param c
     * @param listener
     * @param sheetNo
     * @param headRowNum
     * @return
     */
    public String readExcel(MultipartFile file, Class c, AnalysisEventListener listener, int sheetNo, int headRowNum ){
      return   OperateExcelService.readExcel(file, c, listener, sheetNo, headRowNum);
    }

    /**
     * Excel文件全部读取
     * @param file
     * @param c
     * @param listener
     * @param headRowNum
     * @return
     */
    public String readAllExcel(MultipartFile file, Class c, AnalysisEventListener listener, int headRowNum){
        return OperateExcelService.readAllExcel(file, c, listener, headRowNum);
    }

    /**
     * Excel文件输出
     * @param response
     * @param o
     * @param startPage
     * @param endPage
     * @throws IOException
     */
    public void writeExcel(HttpServletResponse response, Object o, String sheetName, int startPage, int endPage, long size) {
        List data = Data(o, startPage, endPage, size);
        if(o instanceof Asset){
            OperateExcelService.writeExcel(response, Asset.class, sheetName, data);
        }else if(o instanceof Budget){
            OperateExcelService.writeExcel(response, Budget.class, sheetName, data);
        }
    }

    /**
     * 下载选择的数据
     * @param response
     * @param c
     * @param sheetName
     * @param data
     */
    public void writeExcel(HttpServletResponse response, Class c, String sheetName, List<Asset> data){
        List<Asset> assets = new ArrayList<>();
        Long id = new Long(0);
        if(data.size()>0){
            for(Asset a : data){
                switch (a.getStatus()){
                    case "0":
                        a.setStatus("未盘点");
                        break;
                    case "1":
                        a.setStatus("已盘点");
                        break;
                    case "-1":
                        a.setStatus("已报废");
                        break;
                    case "-2":
                        a.setStatus("低值易耗品");
                    default:
                        break;
                }
                a.setId(id++);
                assets.add(a);
            }
        }
        OperateExcelService.writeExcel(response, c, sheetName, assets);
    }


    /**
     * 返回查询结果
     * @param o
     * @param startNo
     * @param endNo
     * @return
     */
    private   List  Data( Object o, int startNo, int endNo, long size){
        List tempList;
        List<Asset> result = new ArrayList<>();
        long i = 1;
        if(o instanceof Asset){
            Asset a = (Asset)o;

            QueryWrapper<Asset> queryWrapper = new QueryWrapper<>();
            //TODO 拼装查询条件
            if(a.getProjectId()!=null&&StringUtil.isNotBlank(a.getProjectId())){
                queryWrapper.eq("project_id", a.getProjectId());
            }
            if(a.getProjectId()!=null&&StringUtil.isNotBlank(a.getAssetCode())){
                queryWrapper.eq("asset_code", a.getAssetCode());
            }
            //获取某个时间阶段内的数据
            if(a.getCreateTime()!=null&& a.getUpdateTime()!=null){
                queryWrapper.between("create_time", a.getCreateTime(), a.getUpdateTime());
            }
            if(a.getStatus()!=null&&!a.getStatus().equals("")){
                queryWrapper.eq("status", a.getStatus());
            }else {
                queryWrapper.between("status","0","1");
            }

            tempList = as.list(queryWrapper);
            //TODO 根据分页情况获取所需数据
            if(size >= tempList.size()){
                tempList = tempList.subList((int) ((startNo-1)*size), tempList.size());
            }else{
                tempList = tempList.subList((int) ((startNo-1)*size), (int)((endNo)*size));
            }

            for(Object object : tempList){
                Asset asset = (Asset)object;
                switch (asset.getStatus()){
                    case "0":
                        asset.setStatus("未盘点");
                        break;
                    case "1":
                        asset.setStatus("已盘点");
                        break;
                    case "-1":
                        asset.setStatus("已报废");
                        break;
                    case "-2":
                        asset.setStatus("低值易耗品");
                    default:
                        break;
                }
                asset.setId(i++);
                result.add(asset);
            }

        }else if (o instanceof Budget){
            Budget b = (Budget)o;
            QueryWrapper<Budget> queryWrapper = new QueryWrapper<>();
            //TODO 拼装查询条件
            if(b.getProjectId()!=null&&!b.getProjectId().isEmpty()){
                queryWrapper.eq("project_id", b.getProjectId());
            }
            if(b.getCompanyId()!=null&&!b.getCompanyId().isEmpty()){
                queryWrapper.eq("company_id", b.getCompanyId());
            }
            tempList = bs.list(queryWrapper);
            if(size >= tempList.size()){
                result = tempList.subList((int) ((startNo-1)*size),tempList.size());
            }else{
                result = tempList.subList((int) ((startNo-1)*size), (int)((endNo)*size));
            }
        }else {
            result  = new ArrayList();
        }
        return result;

    }

}
